package com.dm.library.widgets;

import android.content.Context;
import android.content.res.Resources;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.support.v4.content.res.ResourcesCompat;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;

import com.dm.library.R;
import com.dm.library.utils.DensityUtil;
import com.dm.library.widgets.element.DTextView;


/**
 * 版    权:  深圳市迪蒙网络科技有限公司
 * 描    述:  <描述>
 * Created by sushuo on 2017/02/23.
 */

public class OneLineLinearLayout extends LinearLayout {
    private static final int DEFAULT_TEXT_COLOR = Color.parseColor("#ff666666");
    private static final int DEFAULT_TEXT_SIZE = 14;// sp
    private static final int LEFT_DRAWABLE_PADDING = 10;// dp
    private static final int RIGHT_DRAWABLE_PADDING = 8;// dp
    private static final boolean IS_SHOW_BOTTOM_LINE = true;

    private DTextView rightTextView;
    private DTextView leftTextView;
    private View vBottomLine;
    private boolean isShowBottomLine;

    public OneLineLinearLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView(context, attrs);
    }

    public OneLineLinearLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView(context, attrs);
    }

    public OneLineLinearLayout(Context context) {
        super(context);
        initView(context, null);
    }

    private void initView(Context context, AttributeSet attrs) {
        // attachToRoot传入true代表layout文件填充的View会被直接添加进ViewGroup，而传入false则代表创建的View会以其他方式被添加进ViewGroup。
        View view = LayoutInflater.from(context).inflate(R.layout.one_line_layout, this);// 相当于attachToRoot为true
        rightTextView = (DTextView) view.findViewById(R.id.text_img_right);
        leftTextView = (DTextView) view.findViewById(R.id.text_img_left);
        vBottomLine = view.findViewById(R.id.v_bottom_line);

        if (null != attrs && !isInEditMode()) {
            TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.OneLineLinearLayout);
            isShowBottomLine = typedArray.getBoolean(R.styleable.OneLineLinearLayout_isShowBottomLine, IS_SHOW_BOTTOM_LINE);
            int rightTextColor = typedArray.getColor(R.styleable.OneLineLinearLayout_rightTextColor, DEFAULT_TEXT_COLOR);
            int leftTextColor = typedArray.getColor(R.styleable.OneLineLinearLayout_leftTextColor, DEFAULT_TEXT_COLOR);

            int rightTextSize = typedArray.getDimensionPixelSize(R.styleable.OneLineLinearLayout_rightTextSize, DensityUtil.sp2px(context, DEFAULT_TEXT_SIZE));
            int leftTextSize = typedArray.getDimensionPixelSize(R.styleable.OneLineLinearLayout_leftTextSize, DensityUtil.sp2px(context, DEFAULT_TEXT_SIZE));

            int rightDrawableResourceId = typedArray.getResourceId(R.styleable.OneLineLinearLayout_rightDrawableResourceId, 0);
            int leftDrawableResourceId = typedArray.getResourceId(R.styleable.OneLineLinearLayout_leftDrawableResourceId, 0);

            String rightTextContent = typedArray.getString(R.styleable.OneLineLinearLayout_rightTextContent);
            String leftTextContent = typedArray.getString(R.styleable.OneLineLinearLayout_leftTextContent);

            setTextViewContent(leftTextView, (null != leftTextContent) ? leftTextContent : "", leftDrawableResourceId, true);
            leftTextView.setTextColor(leftTextColor);
            leftTextView.setTextSize(TypedValue.COMPLEX_UNIT_PX, leftTextSize);

            setTextViewContent(rightTextView, null != rightTextContent ? rightTextContent : "", rightDrawableResourceId, false);
//            rightTextView.setHint(null != rightTextHintContent ? rightTextHintContent : "");
            rightTextView.setTextColor(rightTextColor);
//            rightTextView.setHintTextColor(rightTextHintColor);
            rightTextView.setTextSize(TypedValue.COMPLEX_UNIT_PX, rightTextSize);

            int leftDrawablePadding = typedArray.getDimensionPixelSize(R.styleable.OneLineLinearLayout_leftDrawablePadding, DensityUtil.dip2px(context, LEFT_DRAWABLE_PADDING));
            int rightDrawablePadding = typedArray.getDimensionPixelSize(R.styleable.OneLineLinearLayout_rightDrawablePadding, DensityUtil.dip2px(context, RIGHT_DRAWABLE_PADDING));
            leftTextView.setCompoundDrawablePadding(leftDrawablePadding);
            rightTextView.setCompoundDrawablePadding(rightDrawablePadding);

            vBottomLine.setVisibility(isShowBottomLine ? VISIBLE : GONE);
            typedArray.recycle();
        }
    }


    public void isShowBottomLine(boolean showBottomLine) {
        this.isShowBottomLine = showBottomLine;
        vBottomLine.setVisibility(isShowBottomLine ? VISIBLE : GONE);
    }

    /***
     * 设置左边文字
     *
     * @param text
     */
    public void setLeftContent(String text) {
        if (null != leftTextView) {
            setTextViewContent(leftTextView, text, 0, true);
        }
    }

    /***
     * 设置左边图片
     *
     * @param resourceId
     */
    public void setLeftImg(int resourceId) {
        if (null != leftTextView) {
            setTextViewContent(leftTextView, null, resourceId, true);
        }
    }

    /**
     * 清除左边图标
     */
    public void clearLeftImg() {
        if (null != leftTextView) {
            leftTextView.setCompoundDrawables(null, null, null, null);
        }
    }

    /***
     * 设置左边文字，图片
     *
     * @param text
     * @param resourceId
     */
    public void setLeftContent(String text, int resourceId) {
        if (null != leftTextView) {
            setTextViewContent(leftTextView, text, resourceId, true);
        }
    }

    /***
     * 设置右边文字
     *
     * @param text
     */
    public void setRightContent(String text) {
        if (null != rightTextView) {
            setTextViewContent(rightTextView, text, 0, false);
        }
    }

    /***
     * 设置右边文字
     *
     * @param text
     */
    public void setRightContent(double text) {
        if (null != rightTextView) {
            rightTextView.setTextContent(text);
        }
    }


    /***
     * 设置右边图片
     *
     * @param resourceId
     */
    public void setRightImg(int resourceId) {
        if (null != rightTextView) {
            setTextViewContent(rightTextView, null, resourceId, false);
        }
    }

    /**
     * 清除右边图标
     */
    public void clearRightImg() {
        if (null != rightTextView) {
            rightTextView.setCompoundDrawables(null, null, null, null);
        }
    }

    /***
     * 设置右边边文字
     *
     * @param text
     * @param resourceId
     */
    public void setRightContent(String text, int resourceId) {
        if (null != rightTextView) {
            setTextViewContent(rightTextView, text, resourceId, false);
        }
    }

    /***
     * @param textView    控件
     * @param content     内容
     * @param resourceId  资源ID
     * @param leftOrRight 图片在左边还是右边 true 左边
     */
    private void setTextViewContent(DTextView textView, String content, int resourceId, boolean leftOrRight) {
        if (content != null) {
            textView.setTextContent(content);
        }
        if (resourceId <= 0) {
            return;
        }
        try {
            Drawable drawable = ResourcesCompat.getDrawable(getResources(), resourceId, null);
            if (null != drawable) {
                drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());//必须设置图片大小，否则不显示
                if (leftOrRight) {
                    textView.setCompoundDrawables(drawable, null, null, null);
                } else {
                    textView.setCompoundDrawables(null, null, drawable, null);
                }
            } else {
                if (leftOrRight) {
                    textView.setCompoundDrawables(null, null, null, null);
                } else {
                    textView.setCompoundDrawables(null, null, null, null);
                }
            }
        } catch (Resources.NotFoundException e) {
            textView.setCompoundDrawables(null, null, null, null);
        }
    }


    /***
     * @param textView    控件
     * @param content     内容
     * @param resourceId  资源ID
     * @param leftOrRight 图片在左边还是右边 true 左边
     */
    private void setTextViewContent(DTextView textView, double content, int resourceId, boolean leftOrRight) {
        textView.setTextContent(content);
        if (resourceId <= 0) {
            return;
        }
        try {
            Drawable drawable = ResourcesCompat.getDrawable(getResources(), resourceId, null);
            if (null != drawable) {
                drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());//必须设置图片大小，否则不显示
                if (leftOrRight) {
                    textView.setCompoundDrawables(drawable, null, null, null);
                } else {
                    textView.setCompoundDrawables(null, null, drawable, null);
                }
            } else {
                if (leftOrRight) {
                    textView.setCompoundDrawables(null, null, null, null);
                } else {
                    textView.setCompoundDrawables(null, null, null, null);
                }
            }
        } catch (Resources.NotFoundException e) {
            textView.setCompoundDrawables(null, null, null, null);
        }
    }

    public void setRightTextColor(int color) {
        if (null != rightTextView) {
            rightTextView.setTextColor(getResources().getColor(color));
        }
    }

    public void setLeftTextColor(int color) {
        if (null != leftTextView) {
            leftTextView.setTextColor(getResources().getColor(color));
        }
    }

    public String getRightText() {
        return null == rightTextView ? "" : rightTextView.getText().toString();
    }

    public String getLeftText() {
        return null == leftTextView ? "" : leftTextView.getText().toString();
    }
}
